package com.zhuozhou.jdyc.main.user.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;

import androidx.fragment.app.Fragment;

import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.api.AppManager;
import com.zhuozhou.jdyc.api.ConstantsHelper;
import com.zhuozhou.jdyc.api.RegexUtils;
import com.zhuozhou.jdyc.api.UserAccountHelper;
import com.zhuozhou.jdyc.base.BaseActivity;
import com.zhuozhou.jdyc.databinding.LoginBinding;
import com.zhuozhou.jdyc.main.main.activity.MainActivity;
import com.zhuozhou.jdyc.main.user.viewmodel.UserViewModel;
import com.zhuozhou.jdyc.util.apiUtil.StringUtil;
import com.zhuozhou.jdyc.util.log.ToastUtils;
import com.zhuozhou.jdyc.wight.dialog.ConfirmDialog;

/**
 * Created by 方舟 on 2019/8/23.
 * describe：登录，登录方式：账号密码登录
 */
public class LoginActivity extends BaseActivity<UserViewModel, LoginBinding> {

    @Override
    public String setTitleBar() {
        return "登录";
    }//注册

    @Override
    protected int getLayoutId() {
        return R.layout.login;
    }

    @Override
    protected boolean hasToolBar() {
        return false;
    }

    @Override
    public void initView() {
        setThemeBarAndToolBarColor(R.color.default_background);
        binding.tvRegister.setOnClickListener(v -> {
            Intent in = new Intent(this, RegisterActivity.class);
            startActivity(in);
        });
        binding.loginSubmit.setOnClickListener(v -> {
            if (StringUtil.isEmpty(binding.userEdit.getText().toString())) {
                ToastUtils.showShort(v.getContext(), "请填写手机号码");
                return;
            }
            if (!RegexUtils.isMobile(binding.userEdit.getText().toString())) {
                ToastUtils.showShort(v.getContext(), "手机号码不正确");
                return;
            }
            if (StringUtil.isEmpty(binding.passwordEdit.getText().toString())) { //订单
                ToastUtils.showShort(v.getContext(), "请填写密码");
                return;
            }

            UserAccountHelper.setToken("e85b47b7-356e-4d16-9c6f-40ae027cb79f");
            mViewModel.loginPass(binding.userEdit.getText().toString(), binding.passwordEdit.getText().toString());
        });
        mViewModel.BasicsDelShopMutableLiveData.observe(this, v -> {
            ToastUtils.showShort(this, v.getMsg());
            UserAccountHelper.setToken(v.getToken());
            UserAccountHelper.saveLoginState(v, true);
            try {
                UserAccountHelper.saveUserMessage(binding.userEdit.getText().toString(), binding.passwordEdit.getText().toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
            Bundle bundle = new Bundle();
            bundle.putBoolean("result", true);
            setResult(ConstantsHelper.LOGIN_SUCCESS, getIntent().putExtras(bundle));
            if (UserAccountHelper.getLastAccount() != null && !UserAccountHelper.getLastAccount().equals(UserAccountHelper.getAccount())) {
                AppManager.getAppManager().finishAllActivity();
                MainActivity.show(this);
            }
            finish();
        });
    }

    @Override
    public void initData(Bundle bundle) {
    }

    public static void show(Context context, Bundle bundle) {
        Intent intent = new Intent(context, LoginActivity.class);
        intent.putExtras(bundle);
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);//如果存在就不创建，直接打开
        context.startActivity(intent);
    }

    public static void show(Fragment context, Bundle bundle) {
        Intent intent = new Intent(context.getActivity(), LoginActivity.class);
        intent.putExtras(bundle);
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);//如果存在就不创建，直接打开
        context.startActivityForResult(intent, ConstantsHelper.REQUEST_ERROR_LOGIN);
    }

    public static void show(Activity context, Bundle bundle) {
        Intent intent = new Intent(context, LoginActivity.class);
        intent.putExtras(bundle);
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);//如果存在就不创建，直接打开
        context.startActivityForResult(intent, ConstantsHelper.REQUEST_ERROR_LOGIN);
    }

    public static void show(Fragment context, Bundle bundle, int code) {
        Intent intent = new Intent(context.getActivity(), LoginActivity.class);
        intent.putExtras(bundle);
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);//如果存在就不创建，直接打开
        context.startActivityForResult(intent, code);
    }

    public static void show(Activity context, Bundle bundle, int code) {
        Intent intent = new Intent(context, LoginActivity.class);
        intent.putExtras(bundle);
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);//如果存在就不创建，直接打开
        context.startActivityForResult(intent, code);
    }
 
//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            new ConfirmDialog(this)
//                    .setMessage("确定退出当前应用吗？")
//                    .setSureText("退出")
//                    .setOnSureClickListener(dialog -> AppManager.getAppManager().AppExit(LoginActivity.this))
//                    .builder()
//                    .show();
//            return false;
//        }
//        return super.onKeyDown(keyCode, event);
//    }
}
